import time
import win32com
import os
import pythoncom
from win32com.client import Dispatch

from config import BASC


class DocHandle():
    def __init__(self,docxfile):
        self.docxfile=docxfile
        self.myDoc = None
        # 这个是用来储存上一次一样的，防止重复赋值出现。
        self.cuncl = ""

        # 这个表示前面用过了，然后之后的就不再执行了。
        self.tesjx = 0

        # 这个是复制多少次，然后保存的。
        self.jsbcnm = 0
        # jilucwnm=0
        # qcck=0

        # 加入时间
        # tastime=time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())

        # 这个是记录初次打开的，要格式页面word之类的。
        self.testt = 1

        #这是储存在txt文档中的
        self.txtfile=f"{BASC}/word文档/备份粘贴的文本数据.txt"

#这个都不用了，都是直接传入字符的了。
# straby=""
# str_list=[]
    def dete_parg_num(self):
        tc = self.myDoc.Paragraphs.Count
        if tc==1:

            self.myDoc.Paragraphs(int(self.myDoc.Paragraphs.Count)).Range.Paragraphs.Add()
            self.myDoc.Paragraphs(int(self.myDoc.Paragraphs.Count)).Range.Paragraphs.Add()
            self.myDoc.Paragraphs(int(self.myDoc.Paragraphs.Count)).Range.Paragraphs.Add()

    def open_docx(self):
        # tc = self.myDoc.Paragraphs.Count
        # tzs = int(tc) - int(1)
        # self.myDoc.Paragraphs(int(tzs)).Range
        # self.myDoc.Paragraphs(int(self.myDoc.Paragraphs.Count)).Range.Paragraphs.Add()
        # self.myDoc.Paragraphs(int(self.myDoc.Paragraphs.Count)).Range.Paragraphs.Add()
        # self.myDoc.Paragraphs(int(self.myDoc.Paragraphs.Count)).Range.Paragraphs.Add()
        # global myDoc
        if self.myDoc is None:
            wordApp = win32com.client.Dispatch('Word.Application')

            # ~ if testt==1:
            wordApp.Visible = 1
            wordApp.DisplayAlerts = 0
            # pwd = os.getcwd()
            # myDoc = wordApp.Documents.Open(pwd + splittna)
            self.myDoc = wordApp.Documents.Open(os.path.abspath(self.docxfile))


        # return myDoc

    def get_docx_second_format(self,title=None):
        # global myDoc
        self.open_docx()
        if self.testt == 1:
            self.myDoc.Paragraphs(int(self.myDoc.Paragraphs.Count)).Range.Paragraphs.Add()
            self.myDoc.Paragraphs(int(self.myDoc.Paragraphs.Count)).Range.Paragraphs.Add()
            self.myDoc.Paragraphs(int(self.myDoc.Paragraphs.Count)).Range.Paragraphs.Add()

            tc = self.myDoc.Paragraphs.Count
            tzs = int(tc) - int(1)
            myRangef = self.myDoc.Paragraphs(int(tzs)).Range

            # 1厘米约=28.35磅
            myRangef.PageSetup.TopMargin = 5.67
            # os._exit(0)
            myRangef.PageSetup.BottomMargin = 5.67
            myRangef.PageSetup.LeftMargin = 14.175
            myRangef.PageSetup.RightMargin = 14.175
            myRangef.PageSetup.PageWidth = 425.25

            # ~ win32clipboard.CloseClipboard()
            # ~ win32clipboard.OpenClipboard()

            # ~ data = win32clipboard.GetClipboardData()

            # ~ win32clipboard.CloseClipboard()

            self.myDoc.Paragraphs(int(self.myDoc.Paragraphs.Count)).Range.Paragraphs.Add()
            # myDoc.Paragraphs(int(myDoc.Paragraphs.Count)).Range.Paragraphs.Add()
            tc = self.myDoc.Paragraphs.Count
            tzs = int(tc) - int(1)
            myRangef = self.myDoc.Paragraphs(int(tzs)).Range
            tastime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
            if title is not None:
                myRangef.Text = f"{tastime}/{title}>>"
                # jjlpo = self.cuncl + ' \\' + tastime

                fo = open(self.txtfile, "a+", encoding='utf-8')
                fo.write(f"\n{tastime}/{title}>>\n")
                fo.close()
            else:
                myRangef.Text = f"{tastime}"
                fo = open(self.txtfile, "a+", encoding='utf-8')
                fo.write(f"\n{tastime}\n")
                fo.close()
            self.myDoc.Paragraphs(int(self.myDoc.Paragraphs.Count)).Range.Paragraphs.Add()
            self.myDoc.Paragraphs(int(self.myDoc.Paragraphs.Count)).Range.Paragraphs.Add()
            self.testt = 2
        # return myDoc

    def zj_title_format(self,data):
        self.dete_parg_num()
        # 因为你用括号扩住了，可能就不需要换行了
        if (str(data).find("第") != -1 and (str(data).find("章") != -1 or str(data).find("卷") != -1) and
                data != self.cuncl and self.tesjx != 1):
            self.cuncl = data
            if (self.cuncl.find("\n") != -1):
                self.cuncl = self.cuncl.replace("\n", "").replace('\r', 'ks')
                self.cuncl = self.cuncl.replace("ksks", "\n")
                self.cuncl = self.cuncl.replace("ks", "\n")
                # cuncl = cuncl
            self.myDoc.Paragraphs(int(self.myDoc.Paragraphs.Count)).Range.Paragraphs.Add()
            # myDoc.Paragraphs(int(myDoc.Paragraphs.Count)).Range.Paragraphs.Add()
            tc = self.myDoc.Paragraphs.Count
            tzs = int(tc) - int(1)
            myRangef = self.myDoc.Paragraphs(int(tzs)).Range
            tastime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
            myRangef.Text = self.cuncl + ' \\' + tastime
            jjlpo=self.cuncl + ' \\' + tastime

            fo = open(self.txtfile, "a+", encoding='utf-8')
            fo.write(f"{jjlpo}\n")
            fo.close()
            self.myDoc.Paragraphs(int(self.myDoc.Paragraphs.Count)).Range.Paragraphs.Add()
            self.myDoc.Paragraphs(int(self.myDoc.Paragraphs.Count)).Range.Paragraphs.Add()
            self.tesjx = 1

            self.cuncl = data
            # return myDoc,data,cuncl,tesjx

    def copy_word_format(self,data):
        self.dete_parg_num()
        if (self.tesjx != 1):
            if (data != self.cuncl):
                self.cuncl = data

                if (self.cuncl.find("\n") != -1):
                    # self.cuncl = self.cuncl.replace("\n", "").replace('\r', 'ks')
                    # self.cuncl = self.cuncl.replace("ksks", "\n")
                    # self.cuncl = self.cuncl.replace("ks", "\n")
                    self.cuncl = "\n" + self.cuncl
                tc = self.myDoc.Paragraphs.Count
                tzs = int(tc) - int(1)
                # print(tc)
                myRangef = self.myDoc.Paragraphs(int(tzs)).Range

                myRangef.Text = self.cuncl
                fo = open(self.txtfile, "a+", encoding='utf-8')
                fo.write(f"{self.cuncl}\n")
                fo.close()


                self.myDoc.Paragraphs(int(self.myDoc.Paragraphs.Count)).Range.Paragraphs.Add()
                self.myDoc.Paragraphs(int(self.myDoc.Paragraphs.Count)).Range.Paragraphs.Add()
                self.tesjx = 1
                self.cuncl = data


        # return myDoc,data,cuncl,tesjx

    #就是要把数字放在第一个位置，才有参数。
    def gloco(self,aass,title=None):
        # print(f"event:{event}")
        # print(f"aass:{aass}")

        pythoncom.CoInitialize()
        # global cuncl
        # global tesjx
        # global jsbcnm
        # global docxfile
        # global myDoc
        # global testt

        # global straby
        # global kts
        # global jilucwnm
        # global qcck
        # global encodezdy
        # global splittna
        # global pwd
        # global tastime




        # wordApp = win32com.client.Dispatch('Word.Application')
        #
        # # ~ if testt==1:
        # wordApp.Visible = 1
        # wordApp.DisplayAlerts = 0
        # # pwd = os.getcwd()
        # # myDoc = wordApp.Documents.Open(pwd + splittna)
        # myDoc = wordApp.Documents.Open(os.path.abspath(docxfile))

        # if testt == 1:
        #     myDoc=get_docx_second_format(docxfile)
        #     testt = 2
            # pyautogui.hotkey('ctrl', 'c')
            # time.sleep(0.5)
        self.get_docx_second_format(title)
        # pyautogui.hotkey('ctrl', 'c')
        # time.sleep(0.5)
        # win32clipboard.OpenClipboard()

        # data = win32clipboard.GetClipboardData()
        data = aass


        # win32clipboard.CloseClipboard()

        # time.sleep(0.5)

        #因为你用括号扩住了，可能就不需要换行了
        # if (str(data).find("第") != -1 and (str(data).find("章") != -1 or str(data).find("卷") != -1) and
        #         data != cuncl and tesjx != 1):
        #     cuncl = data
        #     if (cuncl.find("\n") != -1):
        #         cuncl = cuncl.replace("\n", "").replace('\r', 'ks')
        #         cuncl = cuncl.replace("ksks", "\n")
        #         cuncl = cuncl.replace("ks", "\n")
        #         # cuncl = cuncl
        #     myDoc.Paragraphs(int(myDoc.Paragraphs.Count)).Range.Paragraphs.Add()
        #     # myDoc.Paragraphs(int(myDoc.Paragraphs.Count)).Range.Paragraphs.Add()
        #     tc = myDoc.Paragraphs.Count
        #     tzs = int(tc) - int(1)
        #     myRangef = myDoc.Paragraphs(int(tzs)).Range
        #     tastime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
        #     myRangef.Text = cuncl + ' \\' + tastime
        #     myDoc.Paragraphs(int(myDoc.Paragraphs.Count)).Range.Paragraphs.Add()
        #     myDoc.Paragraphs(int(myDoc.Paragraphs.Count)).Range.Paragraphs.Add()
        #     tesjx = 1
        #
        #     cuncl = data
        # myDoc, data, cuncl, tesjx=zj_title_format(myDoc, data, cuncl, tesjx)
        self.zj_title_format(data)

        # if (str(data).find("第") != -1 and str(data).find("卷") != -1 and data != cuncl and tesjx != 1):
        #     cuncl = data
        #     if (cuncl.find("\n") != -1):
        #         cuncl = cuncl.replace("\n", "").replace('\r', 'ks')
        #         cuncl = cuncl.replace("ksks", "\n")
        #         cuncl = cuncl.replace("ks", "\n")
        #
        #     myDoc.Paragraphs(int(myDoc.Paragraphs.Count)).Range.Paragraphs.Add()
        #     # myDoc.Paragraphs(int(myDoc.Paragraphs.Count)).Range.Paragraphs.Add()
        #     tc = myDoc.Paragraphs.Count
        #     tzs = int(tc) - int(1)
        #     tastime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
        #     myRangef.Text = cuncl + ' \\' + tastime
        #     myDoc.Paragraphs(int(myDoc.Paragraphs.Count)).Range.Paragraphs.Add()
        #     myDoc.Paragraphs(int(myDoc.Paragraphs.Count)).Range.Paragraphs.Add()
        #     tesjx = 1
        #
        #     cuncl = data

        # if (tesjx != 1):
        #     if (data != cuncl):
        #         cuncl = data
        #
        #         if (cuncl.find("\n") != -1):
        #             cuncl = cuncl.replace("\n", "").replace('\r', 'ks')
        #             cuncl = cuncl.replace("ksks", "\n")
        #             cuncl = cuncl.replace("ks", "\n")
        #             cuncl = "\n" + cuncl
        #         tc = myDoc.Paragraphs.Count
        #         tzs = int(tc) - int(1)
        #         myRangef = myDoc.Paragraphs(int(tzs)).Range
        #
        #         myRangef.Text = cuncl
        #         myDoc.Paragraphs(int(myDoc.Paragraphs.Count)).Range.Paragraphs.Add()
        #         myDoc.Paragraphs(int(myDoc.Paragraphs.Count)).Range.Paragraphs.Add()
        #
        #         cuncl = data
        # myDoc, data, cuncl, tesjx=copy_word_format(myDoc, data, cuncl, tesjx)
        self.copy_word_format(data)

        # kts=-1
        # ~ time.sleep(0.5)

        self.jsbcnm = self.jsbcnm + 1

        # 五分钟保存一次
        #300行保存一次？？
        #复制300次保存一次？？
        if (self.jsbcnm == 1):
            self.myDoc.SaveAs(self.docxfile)
            self.jsbcnm = 0
            # print("保存一次")

        if (self.tesjx == 1):
            self.tesjx = 0

if __name__ == '__main__':
    pass